import type { UserApi } from '../xtgl/user';

import { baseRequestClient, requestClient } from '#/api/request';

export namespace AuthApi {
  /** 登录接口参数 */
  export interface LoginParams {
    password?: string;
    accountName?: string;
    category: number;
    key: string;
    verificationCode: string;
    base64Str: string;
  }

  export interface TokenResult {
    token: string;
    expireTime: string;
    refreshToken: string;
    refreshExpireTime: string;
  }

  export interface RefreshTokenParams {
    token: string;
    refreshToken: string;
  }

  /** 登录接口返回值 */

  export interface LoginResult extends TokenResult {
    userInfo: UserApi.UserInfoResult;
  }
}

/**
 * 登录
 */
export async function loginApi(data: AuthApi.LoginParams) {
  return requestClient.post<AuthApi.LoginResult>('sys/user/login', data);
}

/**
 * 刷新accessToken
 */
export async function refreshTokenApi(params: AuthApi.RefreshTokenParams) {
  return baseRequestClient.post<AuthApi.TokenResult>(
    'sys/common/refresh-token',
    params,
  );
}

/**
 * 退出登录
 */
export async function logoutApi() {
  return baseRequestClient.post('/auth/logout', {
    withCredentials: true,
  });
}

/**
 * 获取用户权限码
 */
export async function getAccessCodesApi() {
  return requestClient.get<string[]>('/auth/codes');
}
